%% MASTER SOLVER
% Sets parameters and solves different versions of the model

%% Timestamp
timestamp = now;

%% Paramters
top_ret = 0.02; 

% Preference Parameters
rho = 1/3; 
r = 0.02;        
ra = 0.05/(1-rho);

% Return Parameters
alpha = 0;
sigma = 0.18;

% Moral Hazard parameter
lambda = 0.95;

%max signal-to-noise ratio
eta_max =  top_ret/sigma;

%Control space
M = 5;
eta_min = 0;

% Check condition for finite cost
eta_max*sigma < lambda*sigma*rho*sqrt(2*(1-rho))*sqrt( (ra - r)*(1-rho)/(rho)  )

% Parametes to feed to external functions
params = [rho, r, ra, alpha, sigma, lambda, M, eta_min, eta_max];

%State Space
n_grid= 101;

phi_min = 0;
phi_max = 1;
PHI = linspace(phi_min,phi_max,n_grid)';
dphi = (phi_max-phi_min)/(n_grid-1);

y_min = 0;
y_max = 1;
Y = linspace(y_min,y_max,n_grid)';
dy = (y_max-y_min)/(n_grid-1);

%Initialize Matrices
PHI_mat = ones(n_grid,1)*PHI';
Y_mat = Y*ones(1,n_grid);
 
% Grid params
grid_params = {n_grid, Y_mat, PHI_mat, dy, dphi};

%Parameters for numerical algorithm
max_iter = 1000;
D = 10000000;
thr = 10^(-11);

num_params = {max_iter, D, thr};

%% Optimal
[V_opt, C_opt, Beta_opt, K_opt, Eta_opt, f_opt, ExRet_opt, mu_v_opt, mu_y_opt, vol_y_opt, vol_phi_opt, mu_k_opt, mu_c_opt, vol_k_opt, vol_c_opt, Suff_opt] = ...
    optimal_system_solver(params, grid_params, num_params, timestamp);

%% Limited commitment
[V_lc, Z_lc, C_lc, Beta_lc, K_lc, Eta_lc, f_lc, ExRet_lc, mu_v_lc, vol_phi_lc, mu_k_lc, mu_c_lc, vol_k_lc, vol_c_lc, Suff_lc] = ...
    limited_system_solver(params, grid_params, num_params);

%% Continuum of known types
[V_type, C_type, Beta_type, K_type, Eta_type, f_type, ExRet_type, mu_v_type] = ...
    type_system_solver(params, grid_params, num_params);


